<?php
namespace Admin\Controller;
use Think\Controller;
class QuitController extends \Org\Util\AdminController {
    protected function _initialize() {
        $system['name']="交费系统";
        $this->assign('system',$system);
    }
    public function QuitR($number = '', $name = '',$identity = '',$state = '',$term_id = '',$student_id="") {
        $this->assign('modelIcon', 'icon-user');
        $crumbsMap['QuitR'] = '信息'; 
        $this->assign('crumbsMap', $crumbsMap);
        $this->assign('modelName', '退费');
          $header = array(
            'sup'=>array(
                'name'=>'退费管理',
                'quit'=>'active',
            ),
            'sub'=>array(
                'name'=>'退费列表',
                'quit_r'=>'active',
            ),
            'icon'=>'icon-usd',
            'url'=>'QuitR',
        );
        $this->assign('header', $header);
        
        //查询功能
        $findFlag = 0;
        if(($state != '')&&($findFlag == 0)){
            $data=[];
            $student=D('Student')->where(['state'=>$state])->select();
            foreach ($student as $k=>$v){
                $data[]=$v['id'];
            }
            if($data){
                $findMap['student_id'] = ['in',$data];
                $findFlag = 1;
            }
        } 
        
        if (($number != '')&&($findFlag == 0)) {
            $student = M('Student')->where(['number'=>$number])->find();
            $findMap['student_id'] = ['eq', $student['id']];
        }
        if (($term_id != '')&&($findFlag == 0)) {
            $findMap['term_id'] = ['LIKE', '%'.$term_id.'%'];
            
        }
         if (($student_id != '')&&($findFlag == 0)) {
            $findMap['student_id'] = ['LIKE', '%'.$student_id.'%'];
            
        }
        if (($identity != '')&&($findFlag == 0)) {
            $student = M('Student')->where(['identity'=>$identity])->find();
            $findMap['student_id'] = ['eq', $student['id']];
            
        }
        if (($name != '')&&($findFlag == 0)) {
            $student = M('Student')->where(['name'=>$name])->find();
            $findMap['student_id'] = ['eq', $student['id']];
            
        }
        //分页获取信息
        $model = D('Fee');
        $count = $model->where($findMap)->count();
        $page = new \Think\AdminPage($count, C('PAGE_NUM'));
        $show = $page->show();
        $list = $model->where($findMap)->order('id')->limit($page->firstRow . ',' . $page->listRows)->select();
        foreach ($list as $key => $value) {
            $list[$key]['fee'] = D('Student')->where(['id'=>$value['student_id']])->find();
        }
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show);
        $term = M('Term')->select();
        $this->assign('term', $term);
        $this->display('quit_r'); 
    }
    public function QuitU($id=0) {
        $this->assign('modelIcon', 'icon-user');
        $crumbsMap['QuitR'] = '信息'; 
        $this->assign('crumbsMap', $crumbsMap);
        $this->assign('modelName', '退费');
        
        $student = D('Student');
        $fee = D('Fee');
        $list=$fee->where(['id'=>$id])->find();
        $tuition=D('Tuition')->where(['id'=>$list['tuition_id']])->find();
        $this->assign('tuition',$tuition);
        $model=$student->where(['id'=>$list['student_id']])->find();
        $this->assign('model',$model);
        $this->assign('list',$list);
        if (IS_POST) {
            $_POST['id'] = $id;
            if ($_POST['quit'] > $_POST['over']) {
                $this->error('退还学费不能高出实交学费，请检查!');
                exit();
            }
            if (!$fee->create()) {
                $this->error($fee->getError());
                return false;
            }
            $fee->save();
            $this->success('修改成功!', U('Quit/QuitR'));
        }
       
        $this->display('quit_u');
    }
    public function QuitO($number = '', $name = '',$identity = '',$state = '',$term_id = '') {
        //查询信息
       $findFlag = 0;
        if(($state != '')&&($findFlag == 0)){
            $data=[];
            $student=D('Student')->where(['state'=>$state])->select();
            foreach ($student as $k=>$v){
                $data[]=$v['id'];
            }
            $findMap['student_id'] = ['in',$data];
            $findFlag = 1;
        } 
        
        if (($number != '')&&($findFlag == 0)) {
            $student = M('Student')->where(['number'=>$number])->find();
            $findMap['student_id'] = ['eq', $student['id']];
        }
        if (($term_id != '')&&($findFlag == 0)) {
            $findMap['term_id'] = ['LIKE', '%'.$term_id.'%'];
            
        }
        if (($identity != '')&&($findFlag == 0)) {
            $student = M('Student')->where(['identity'=>$identity])->find();
            $findMap['student_id'] = ['eq', $student['id']];
            
        }
        if (($name != '')&&($findFlag == 0)) {
            $student = M('Student')->where(['name'=>$name])->find();
            $findMap['student_id'] = ['eq', $student['id']];
            
        }
        $model = M('Fee');
        $list = $model->where($findMap)->order('id')->limit($page->firstRow.','.$page->listRows)->select();
        $xlsName = "退费";
        
        $xlsCell = [
            ["id","ID"],
            ["student_id","学生"],
            ["class_id","所在班级"],
            ["term_id","学期"],
            ["should","应交学费"],
            ["over","实交学费"],
            ["quit","退费"],
            ["create_time","创建时间"],
            ["update_time","更新时间"],
        ];
        $model = M('Fee');
        $xlsData = $model->where($findMap)->field('id,student_id,tuition_id,over,quit,create_time,update_time')->order('id')->select();
          foreach ($xlsData as $k=>$v){
            $student=D('student')->where(['id'=>$v['student_id']])->find();
            $class=D('Class')->where(['id'=>$student['class_id']])->find();
            $grade=D('grade')->where(['id'=>$class['grade_id']])->find();
            $major=D('major')->where(['id'=>$class['major_id']])->find();
            $tuition=D('tuition')->where(['id'=>$v['tuition_id']])->find();
            $term=D('term')->where(['id'=>$tuition['term_id']])->find();
            unset($map);
            $map['id'] = $v['id'];
            $map['student_id'] =$student['name'];
            $map['class_id'] = $grade['name'].$major['name'].$class['name'];
            $map['term_id'] = $term['name'];
            $map['should'] = $tuition['money'];
            $map['over'] = $v['over'];
            $map['quit'] = $v['quit'];
            $map['create_time'] =date('Y-m-d',$v['create_time']);
            $map['update_time'] = date('Y-m-d',$v['update_time']);
            $newData[]=$map;
        }
        \exportExcel($xlsName, $xlsCell, $newData);
    }
    
}
        
